const { ccclass, property } = cc._decorator;

import s from "./AudioManager"
import c from "./PeripheralSystemDataMgr"
import u from "./EventMgr"
import p from "./GameEventType"
import d from "./frameworkManager"
import h from "./AdManager"
import f from "./EngineUtil"
import g, { AnimType } from "./BasePageCtrl"
import y from "./PageMgr"
import m, { TravelEvent } from "./PeripheralSystemMgr"
import _ from "./UiManage"
import v from "./ScrapingCardPage"
import b from "./ScrapingCardRewardtemCtrl"
import UiManager from "./UiManage";

@ccclass
export default class ScrapingCardPageCtrl extends g {
    
    public isCanScrap = false;
    public tempDrawPoints = [];
    public polygonPointsList = [];
    public travelEventData = null;
   
    public onLoad() {
        this.onUILoad();
        this._animType = AnimType.LEFT_TO_RIGHT;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this._showWinSize = true;
        super.onLoad();
        this.addButtonListen();
        this.addEvent();
    };
    public start() { };
    public _init(e?) {
        this.ui.btn_video.active = true;
        this.isCanScrap = false;
        this.tempDrawPoints = [];
        this.polygonPointsList = [];
        this.travelEventData = null;
        this.reset();
        s.getInstance().playMusic("eventOut");
        e && this.initReward(e);
    };
    public onUILoad() {
        this.ui = this.node.addComponent(v);
    };
    public addEvent() {
        this.ui.ticketNode.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.ui.ticketNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.ui.ticketNode.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        this.ui.ticketNode.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        u.listen(p.BEGIN_SCRAP_CARD, this.initReward, this);
    };
    public onDestroy() {
        this.ui.ticketNode.off(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.ui.ticketNode.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.ui.ticketNode.off(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        this.ui.ticketNode.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
    };
    public touchStartEvent(e) {
        if (this.isCanScrap) {
            var t = this.ui.ticketNode.convertToNodeSpaceAR(e.getLocation());
            this.clearMask(t);
        } else d.log("不可以刮");
    };
    public touchMoveEvent(e) {
        if (this.isCanScrap) {
            var t = this.ui.ticketNode.convertToNodeSpaceAR(e.getLocation());
            this.clearMask(t);
        } else d.log("不可以刮");
    };
    public touchEndEvent() {
        if (this.isCanScrap) {
            this.tempDrawPoints = [];
            this.calcProgress();
        } else d.log("不可以刮");
    };
    public calcProgress() {
        var e = 0;
        this.polygonPointsList.forEach(function (t) {
            t.isHit && (e += 1);
        });
        d.log("当前的进度", "已经刮开了 " + Math.ceil(e / this.polygonPointsList.length * 100) + "%");
        var t = e / this.polygonPointsList.length;
        d.log("当前的进度", t);
        debugger
        t >= .6 && y.showPage("TravelEventRewardPage", this.travelEventData);
    };
    public clearMask(e) {
        var t = this.ui.ticket_mask.getComponent(cc.Mask)._graphics, a = this.tempDrawPoints.length;
        this.tempDrawPoints.push(e);
        if (a <= 1) {
            t.circle(e.x, e.y, 90);
            t.fill();
            this.polygonPointsList.forEach(function (t) {
                if (!t.isHit) {
                    var a = e.x > t.rect.x && e.x < t.rect.x + t.rect.width, o = e.y > t.rect.y && e.y < t.rect.y + t.rect.height;
                    a && o && (t.isHit = true);
                }
            });
        } else {
            var o = this.tempDrawPoints[a - 2], i = this.tempDrawPoints[a - 1];
            t.moveTo(o.x, o.y);
            t.lineTo(i.x, i.y);
            t.lineWidth = 180;
            t.lineCap = cc.Graphics.LineCap.ROUND;
            t.lineJoin = cc.Graphics.LineJoin.ROUND;
            t.strokeColor = cc.color(255, 255, 255, 255);
            t.stroke();
            this.polygonPointsList.forEach( (e) => {
                e.isHit = e.isHit || cc.Intersection.lineRect(o, i, e.rect);
            });
        }
    };
    public reset() {
        this.ui.ticket_mask.getComponent(cc.Mask)._graphics.clear();
        this.tempDrawPoints = [];
        this.polygonPointsList = [];
        this.ui.ticketNode.getComponent(cc.Graphics).clear();
        for (var e = 0; e < this.ui.ticketNode.width; e += 180) for (var t = 0; t < this.ui.ticketNode.height; t += 180) this.polygonPointsList.push({
            rect: cc.rect(e - this.ui.ticketNode.width / 2, t - this.ui.ticketNode.height / 2, 180, 180),
            isHit: false
        });
    };
    public addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
        UiManager.addButtonListen(this.ui.btn_video, this.clickVideo, this);
    };
    public clickClose() {
        this.hide();
        if (this.travelEventData) y.showPage("TravelEventRewardPage", this.travelEventData); else {
            c.checkNextOutSideGuide();
            c.checkShowGameStartPage();
        }
    };
    public clickVideo() {
        c.reportEncourageVideo();
        d.log("播放视频======");
        h.getInstance().playNormalVideoAd(function () {
            m.getVideoReward(TravelEvent.SCRAP_CARD, 1, function () { });
        }, function () { }, {
            ad_type: "ScrapingCardPageVideo"
        });
    };
    public async initReward(e: any) {
        if (!e) {
            d.log("初始化奖励不正确");
            return;
        }
        this.travelEventData = e;
        this.ui.rewardNode.removeAllChildren();
        const t = this.getRewardData();
        d.log("获取的奖励数据", t);
        const a = await UiManager.loaderPrefab("ScrapingCardRewardtem");
        for (let o = 0; o < t.length; o++) {
            const i = t[o];
            const r = cc.instantiate(a);
            this.ui.rewardNode.addChild(r);
            r.addComponent(b);
            r.getComponent(b).initData(i);
        }
        this.isCanScrap = true;
        this.ui.btn_video.active = false;
        this.reset();
    }
    public getRewardData() {
        var e = this.travelEventData.reward, t = 0;
        e < 300 ? t = 1 : e >= 300 && e < 600 ? t = 2 : e >= 600 && e < 900 ? t = 3 : e >= 900 && e < 1500 ? t = 4 : e >= 1500 && e < 2e3 ? t = 5 : e >= 2e3 && (t = 6);
        var a = this.randGenerator(t, e);
        d.log("生成的红包金额", a);
        for (var o = [], i = f.randomsInt(2, 7, 6 - t), r = 0; r < t; r++) i.unshift(1);
        for (r = 0; r < i.length; r++) {
            var n = i[r], l = {
                reward_type: n,
                reward_count: 0
            };
            1 == n && (l.reward_count = a[r]);
            o.push(l);
        }
        return f.shuffle(o);
    };
    public randGenerator(e, t) {
        for (var a = [], o = t, i = 0, r = 0; r < e - 1; r++) {
            var n = Math.ceil(Math.random() * (o / 2));
            a.push(n);
            o -= n;
            i += n;
        }
        a.push(t - i);
        return a;
    };
    static prefabUrl = "ScrapingCardPage";
    static className = "ScrapingCardPageCtrl";
    // return r([T, M("UI/pages/ScrapingCardPageCtrl")], t);
}
